﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using AwesomeEnginePC.Physics;

using AwesomeEnginePC.Graphics.TwoDimensional.Text;
using AwesomeEnginePC.Graphics.TwoDimensional.Sprites;

namespace AwesomeEnginePC.Graphics.TwoDimensional.Text.TextEffects
{
    public class TextEffects : BaseString //currently only works with TextShadow
    {
        
        public enum DrawState
        {
            DrawTextShadow,
            DrawBackGroundColor,
            DrawTextBox,
            DrawWrapText,
            DrawScrollingText,
            DrawFadingText,
            DrawUnderlineText,
        }
        struct DisplayMessage
        {
            public string Message;
            public TimeSpan DisplayTime;
            public int CurrentIndex;
            public string DrawnMessage;
            public Vector2 Position;
            public Color color;
            public DisplayMessage(string message, TimeSpan displayTime, Vector2 position, Color thecolor)
            {
                Message = message;
                DisplayTime = displayTime;
                CurrentIndex = 0;
                Position = position;
                color = thecolor;
                DrawnMessage = string.Empty;
            }
        }
        
        public interface DrawEffects
        {
            DrawState State { get; }
        }

        private DrawState effects;
        private List<DisplayMessage> messageList = new List<DisplayMessage>();
        private Rectangle textBox;
        private Color TextBoxColor;
        private Color BackGroundHighLightColor = Color.White;
        private Color ShadowColor = Color.Black;
        private int MaxLineWidth = 0;
        private Vector DropShadowRelitivePosition;
        

        public TextEffects(ContentManager CM, string assetName, string text)
            : base(CM, assetName, text)
        {
        }

        public TextEffects(ContentManager CM, string assetName, string text, Vector position)
            : base(CM, assetName, text, position)
        {
        }

        public TextEffects(ContentManager CM, string assetName, string text, Vector position, Color tint)
            : base(CM, assetName, text, position, tint)
        {

        }

        public TextEffects(ContentManager CM, string assetName, string text, Vector position, Color tint, Vector scaling, float rotation)
            : base(CM, assetName, text, position, tint, scaling, rotation)
        {

        }

        public override void Update(GameTime gameTime)
        {
            switch (effects)
            {
                case DrawState.DrawWrapText:
                    //Do nothing
                    break;

                case DrawState.DrawUnderlineText:
                    //Do nothing
                    break;



                case DrawState.DrawTextShadow:
                    //Do nothing
                    break;

                case DrawState.DrawScrollingText:
                    //UpdateScrollingText(gameTime);
                    break;

                case DrawState.DrawFadingText:

                    break;

                case DrawState.DrawBackGroundColor:
                    //Do nothing
                    break;

                default:

                    break;
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch SB)
        {

            switch (effects)
            {
                case DrawState.DrawWrapText:
                    string text = parseWordText(MaxLineWidth);
                    SB.DrawString(FontSprite, text, Position.ToVector2(), Tint);
                    break;

                case DrawState.DrawUnderlineText:
                    
                    break;

                case DrawState.DrawTextBox:
                     Texture2D dummyTextureTextBox = new Texture2D(graphicsDevice, 1, 1);
                     dummyTextureTextBox.SetData(new Color[] { TextBoxColor });

                     SB.Draw(dummyTextureTextBox, textBox, TextBoxColor);
                     SB.DrawString(FontSprite, wrapTextBoxText(), new Vector2(textBox.X, textBox.Y), Tint);
                    break;

                case DrawState.DrawTextShadow:
                     Vector _Offset = Position + DropShadowRelitivePosition;

                    //TextShadow
                     SB.DrawString(FontSprite, Text, _Offset.ToVector2(), ShadowColor, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);
                     SB.DrawString(FontSprite, Text, Position.ToVector2(), Tint, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);
                    break;

                case DrawState.DrawScrollingText:
                    ScrollingText(gameTime, SB);
                    break;

                case DrawState.DrawFadingText:

                    break;

                case DrawState.DrawBackGroundColor:
                    Color myTransparentColor = new Color(0, 0, 0, 127);

                    Rectangle backgroundRectangle = new Rectangle();
                    backgroundRectangle.Width = (int)Width + 10;
                    backgroundRectangle.Height = (int)Height + 10;
                    backgroundRectangle.X = (int)Position.X - 5;
                    backgroundRectangle.Y = (int)Position.Y - 5;

                    Texture2D dummyTexture = new Texture2D(graphicsDevice, 1, 1);
                    dummyTexture.SetData(new Color[] { BackGroundHighLightColor });

                    SB.Draw(dummyTexture, backgroundRectangle, BackGroundHighLightColor);
                    SB.DrawString(FontSprite, Text, Position.ToVector2(), Tint);
                    break;

                default:

                    break;
            }

        }
       

        /// <summary>
        /// Draws a text Shadow Offset to the Current Text
        /// </summary>
        /// <param name="gameTime"></param>
        /// <param name="sb"></param>
        public void SetShadowTextEffect(Color shadow,Vector dropShadowPos)
        {
            ShadowColor = shadow;
            DropShadowRelitivePosition = dropShadowPos;
            effects = DrawState.DrawTextShadow;
        }

        
        /// <summary>
        /// Creates a BackGround Color around the Text.
        /// </summary>
        /// <param name="gameTime">gametime</param>
        /// <param name="GD">Graphics device for the Dummy Texture</param>
        /// <param name="sb">The SpriteBatch</param>
        /// <param name="backGroundcolor">The Background Color you want for the Text</param>
        public void SetBackGroundColorEffect(Color theColor)
        {
            BackGroundHighLightColor = theColor;
            effects = DrawState.DrawBackGroundColor;
           
        }

        public void SetTextBoxEffect(int posX, int posY, int width, int height,Color textBoxColor)
        {
            textBox = new Rectangle(posX, posY, width, height);
            TextBoxColor = textBoxColor;
            wrapTextBoxText();
            effects = DrawState.DrawTextBox;
           
        }
        /// <summary>
        /// Wraps Around the Text based on the maxLineWidth you want
        /// </summary>
        /// <param name="gameTime"></param>
        /// <param name="sb"></param>
        /// <param name="maxLineWidth"></param>
        public void SetWrapEffect(int lineMaxSize)
        {
            effects = DrawState.DrawWrapText;
            MaxLineWidth = lineMaxSize;
        }
        private string wrapTextBoxText()
        {
            String line = String.Empty;
            String returnString = String.Empty;
            String[] wordArray = Text.Split(' ');

            foreach (String word in wordArray)
            {

                if (FontSprite.MeasureString(line + word).Length() > textBox.Width)
                {
                    returnString = returnString + line + '\n';
                    line = String.Empty;

                }

                line = line + word + ' ';
            }
            return returnString + line;
        }
       

        public void SetScrollingTextEffect(float thedisplayTime)
        {
            effects = DrawState.DrawScrollingText;
            messageList.Add(new DisplayMessage(Text, TimeSpan.FromSeconds(thedisplayTime), Position.ToVector2(), Tint));
        }

        public void SetFadingTextEffect(GameTime gameTime, SpriteBatch sb)
        {
            effects = DrawState.DrawFadingText;
            //Modifiy this
           // sb.DrawString(FontSprite, Text, Position.ToVector2(), Color.White);
        }


        public void SetUnderLineTextEffect(GameTime gameTime, SpriteBatch sb)
        {
            effects = DrawState.DrawUnderlineText;
            //Modifiy this
            sb.DrawString(FontSprite, Text, Position.ToVector2(), Color.White);
        }
        //public void UpdateScrollingText(GameTime gameTime)
        //{
        //    if (messageList.Count > 0)
        //    {
        //        for (int i = 0; i < messageList.Count; i++)
        //        {
        //            DisplayMessage dm = messageList[i];
        //            dm.DisplayTime -= gameTime.ElapsedGameTime;
        //            if (dm.DisplayTime <= TimeSpan.Zero)
        //            {
        //                messageList.RemoveAt(i);
        //            }
        //            else
        //            {
        //                messageList[i] = dm;
        //            }
        //        }
        //    }
        //}
        public void ScrollingText(GameTime gameTime, SpriteBatch sb)
        {
            if (messageList.Count > 0)
            {
                for (int i = 0; i < messageList.Count; i++)
                {
                    DisplayMessage dm = messageList[i];
                    dm.DisplayTime -= gameTime.ElapsedGameTime;

                    dm.DrawnMessage += dm.Message[dm.CurrentIndex].ToString();
                    sb.DrawString(FontSprite, dm.DrawnMessage, dm.Position, dm.color);
                    if (dm.CurrentIndex != dm.Message.Length - 1)
                    {
                        dm.CurrentIndex++;
                        messageList[i] = dm;
                    }

                }
            }
        }
    }//end class
}//end namespace


